package com.qs.eggs.view.prodetail.fragment;

import android.graphics.Color;
import android.support.v4.app.Fragment;
import android.support.v4.content.ContextCompat;
import android.support.v4.view.ViewPager;
import android.view.View;
import android.widget.Toast;

import com.google.gson.Gson;
import com.lzy.okgo.OkGo;
import com.lzy.okgo.callback.StringCallback;
import com.qs.eggs.Constants;
import com.qs.eggs.R;
import com.qs.eggs.adapter.EvaluationTabIndicatorAdapter;
import com.qs.eggs.base.fragment.BaseFragment;
import com.qs.eggs.listener.EvaluationOnTransitionListener;
import com.shizhefei.view.indicator.FixedIndicatorView;
import com.shizhefei.view.indicator.IndicatorViewPager;

import org.json.JSONArray;
import org.json.JSONException;

import java.util.ArrayList;
import java.util.HashMap;

import okhttp3.Call;
import okhttp3.Response;

/**
 * A simple {@link Fragment} subclass.
 */
public class EvaluationFragment extends BaseFragment {
    private FixedIndicatorView indicator;
    private IndicatorViewPager indicatorViewPager;
    private String[] tabName = new String[] {"全部","好评", "中评","差评"};
    private ArrayList<Fragment> fmList;
    private ViewPager viewPager;
    private  String proid,type;
    private String[] mEvaCount;
    ArrayList<HashMap<String,String>> mListCount;
    @Override
    protected int getLayoutId() {
        return R.layout.fragment_evaluation;
    }

    @Override
    protected void initWidget(View root) {
        super.initWidget(root);
        proid=getActivity().getIntent().getStringExtra("proid");
        type=getActivity().getIntent().getStringExtra("type");
        indicator=findView(R.id.guide_indicator);
        viewPager=findView(R.id.guide_viewPager);
        fmList=new ArrayList<>();
        fmList.add(new AllEvaluationFragment());
        fmList.add(new GoodEvaluationFragment());
        fmList.add(new MiddleEvaluationFragment());
        fmList.add(new BadEvaluationFragment());
        // 将viewPager和indicator使用
        indicatorViewPager = new IndicatorViewPager(indicator, viewPager);
        getEvaCount();

    }

    private void ViewPagerSetting() {

        // 未选中字体大小
        float unSelectSize = 14;
        // 选中字体大小
        float selectSize = unSelectSize * 1.0f;
        // 未选中的颜色
        int unSelectColor = Color.parseColor("#555555");
        // 选中时的颜色
        int selectColor = ContextCompat.getColor(getContext(),R.color.white);
        // 设置tab字体的变化
        indicator.setOnTransitionListener(new EvaluationOnTransitionListener(
                selectSize, unSelectSize, selectColor, unSelectColor));

        viewPager.setOffscreenPageLimit(4);
        indicatorViewPager = new IndicatorViewPager(indicator, viewPager);
        // 设置适配器
        indicatorViewPager.setAdapter(new EvaluationTabIndicatorAdapter(getChildFragmentManager(),getContext(),fmList,tabName,mListCount,mEvaCount));
        indicatorViewPager.setOnIndicatorPageChangeListener(new IndicatorViewPager.OnIndicatorPageChangeListener() {
            @Override
            public void onIndicatorPageChange(int preItem, int currentItem) {
                viewPager.setCurrentItem(currentItem);
            }
        });
    }

    private void getEvaCount(){
        final HashMap<String,String> map=new HashMap<>();
        map.put("proid",proid);
        map.put("type",type);
        Gson gson=new Gson();
        String params=gson.toJson(map);
        OkGo.post(Constants.URL_BASE+"evaulteManage?action=evalutecount&table=evaluate")
                .params("params",params)
                .execute(new StringCallback() {
                    @Override
                    public void onSuccess(String s, Call call, Response response) {
                        mEvaCount=new String[3];
                        if("null".equals(s)){
                            Toast.makeText(getContext(),"数量加载失败",Toast.LENGTH_SHORT).show();
                          //  return;
                        }
                        try {
                            JSONArray jsonArray=new JSONArray(s);
                            mListCount=new ArrayList<HashMap<String, String>>();
                            for(int i=0;i<jsonArray.length();i++){
                                mEvaCount[0]=jsonArray.getJSONObject(i).getString("good");
                                mEvaCount[1]=jsonArray.getJSONObject(i).getString("middle");
                                mEvaCount[2]=jsonArray.getJSONObject(i).getString("bad");
                                HashMap<String,String> map1=new HashMap<String, String>();
                                map1.put("good",jsonArray.getJSONObject(i).getString("good"));
                                map1.put("middle",jsonArray.getJSONObject(i).getString("middle"));
                                map1.put("bad",jsonArray.getJSONObject(i).getString("bad"));
                                mListCount.add(map1);
                            }
                            ViewPagerSetting();
                        } catch (JSONException e) {
                            e.printStackTrace();
                        }

                    }
                });
    }
}
